package cn.shenlong.portal.service.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.shenlong.common.Page;
import cn.shenlong.portal.domain.Member;
import cn.shenlong.portal.domain.MemberCard;
import cn.shenlong.portal.mapper.MemberCardMapper;
import cn.shenlong.portal.service.MemberCardService;
import cn.shenlong.portal.service.MemberService;

@Service("MemberCardService")
public class MemberCardServiceImpl implements MemberCardService {

	@Autowired
	private MemberCardMapper MemberCardMapper;
	
	@Autowired
	private MemberService memberService;

	@Override
	public void save(MemberCard memberCard, long memberCardId) {
		MemberCardMapper.saveEntity(memberCard);
	}

	@Override
	public Page<MemberCard> findPageByParm(Map<String, Object> otherParm, Integer pageNo, Integer pageSize) {
		Page<MemberCard> p = new Page<MemberCard>();
		List<MemberCard> memberCardList = MemberCardMapper.findPageListByCriteria(otherParm, initRowBounds(pageSize, pageNo, p));
		Long count = MemberCardMapper.findCountByCriteria(otherParm);
		p.setList(memberCardList);
		p.setTotalCount(count.intValue());
		return p;
	}

	private RowBounds initRowBounds(Integer pageSize, Integer pageNo, Page<MemberCard> p) {
		RowBounds rowBounds = null;
		if (pageSize == null || pageNo == null) {
			rowBounds = new RowBounds();
		} else {
			p.setPageNo(pageNo);
			p.setPageSize(pageSize);
			rowBounds = new RowBounds(p.getFirstResult(), p.getPageSize());
		}
		return rowBounds;
	}
	
	@Override
	public Long getCountByParm(Map<String, Object> otherParm) {
		return MemberCardMapper.findCountByCriteria(otherParm);
	}

	@Override
	public MemberCard findMemberCardByMemberCardId(long memberCardId) {
		return MemberCardMapper.findByPK(memberCardId);
	}

	@Override
	public MemberCard updateMemberCard(MemberCard memberCard) {
		int i = MemberCardMapper.updateEntity(memberCard);
		 if(i<0) return null;
		 return MemberCardMapper.findByPK(memberCard.getMemberCardId());
	}

	@Override
	public void deleteMemberCard(Long[] memberCardId) {
		MemberCardMapper.deleteByPKArray(memberCardId);
	}

	@Override
	public void deleteByMemberCardId(List<Long> MemberCardId) {
		MemberCardMapper.deleteByPKArray((Long[]) MemberCardId.toArray());
	}

	@Override
	public void save(MemberCard memberCard) {
		MemberCardMapper.saveEntity(memberCard);
	}
	
	@Override
	public MemberCard findMemberCardByCode(String memberCode) {
		return MemberCardMapper.findMemberCardByCode(memberCode);
	}
	
	@Override
	public MemberCard findMemberCardByMemberId(Long memberId) {
		return MemberCardMapper.findMemberCardByMemberId(memberId);
	}
	
	public int unbind(Long memberCardId) {
		return MemberCardMapper.unbind(memberCardId);
	}
	
	@Transactional
	public void unbind(MemberCard memberCard) {
		if (memberCard != null) {
			if (memberCard.getMemberId() != null) {
				Member member = this.memberService.findMemberByMemberId(memberCard.getMemberId());
				if (member != null) {
					this.unbind(memberCard.getMemberCardId());
					
					this.memberService.unbind(member.getMemberId());
				} else {
					throw new RuntimeException("操作失败，当前会员卡尚未与会员绑定");	
				}
			} else {
				throw new RuntimeException("操作失败，当前会员卡尚未与会员绑定");
			}
		} else {
			throw new RuntimeException("操作失败，会员卡为空");
		}
	}
}
